$(function() {
	// body...

		$.ajax({
        url: severUrl+'index.php?m=Web&a=product',
        type: 'GET',
        dataType: 'JSONP'
    })
    .done(function(data) {
        console.log("success");
        console.log(data);
        product_slidesFun(data.product_slides);

        our_productsFun(data.our_products.products);
        listKutitle(data.our_products.title);
        listKuseo_keywords(data.our_products.seo_keywords);

        best_sellresFun(data.best_sellres);
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });









// product_slides
function product_slidesFun(data){
	var featuredCon = $('#featuredCon');
	var html = '';

	for (var i = 0; i < data.length; i++) {
		 html += '<div class="swiper-slide"><div class="head">'+data[i].title+'</div><div class="shopList">'+forNei(data[i].products)+'</div><div class="imgC"><img src="'+data[i].image_url+'" alt=""></div></div>';
	}
	featuredCon.html(html);
	 featuredSweiper();

	function forNei(list){
		var htm = ''
		for (var i = 0; i < list.length; i++) {
			htm+='<a href="article.html?article_id='+list[i].id+'"><img src="'+list[i].image_url+'" alt=""></a>';
		}
		return htm;
	}
	function featuredSweiper(){
		var featuredSweiper = new Swiper ('#featuredSweiper', {
           pagination: '.swiper-pagination',

       }) 
	}
	      
}
// product_slides end






// listKutitle
function listKutitle(data){
	var con = $('#OneSearch');
	var html = '';
	for (var i = 0; i < data.length; i++) {
		html+= '<option value="'+data[i].title+'">'+data[i].title+'</option>';
	}

	con.html(html);
}
// listKutitle end



// listKuseo_keywords
function listKuseo_keywords(data){
	var con = $('#TwoSearch');
	var html = '';
	for (var i = 0; i < data.length; i++) {
		html+= '<option value="'+data[i].seo_keywords+'">'+data[i].seo_keywords+'</option>';
	}
	con.html(html);
}
// listKuseo_keywords end






// our_productsFun
function our_productsFun(data){
	var productsList = $('#productsList');
		var html = '';
		for (var i = 0; i < data.length; i++) {
			html += '<li><a href="article.html?article_id='+data[i].article_id+'" class="clearfloat"><div class="imgC"><img src="'+data[i].image_url+'"></div><div class="word"><div class="head">'+data[i].title+'</div><div class="describe">'+data[i].author+'</div><p>'+data[i].seo_description+'</p></div></a></li>';
		}
		productsList.html(html);
}

// our_products end


function selectSearch(){
	var inoutCn = $('#TruiSearch').find('input');
	$('#OneSearch').on('change',function(){
		console.log($(this).val());
		Search($(this).val(),$('#TwoSearch').val())
	})
	$('#TwoSearch').on('change',function(){
		console.log($(this).val());
		Search($('#OneSearch').val(),$(this).val())
	})
	$('#TruiSearch').on('click','.iconC',function(){
		Search('seo_keywords',inoutCn.val());
	})


	inoutCn.focus(function(){
			inoutCn.bind('keydown',function(event){
				console.log(event.keyCode);
				if (event.keyCode == 13) {
					var ind = input.val()
					if (ind.length != 0) {
						Search('seo_keywords',ind);
					}
				}
			})
		});

		inoutCn.blur(function(){
			inoutCn.unbind('keydown')
		})





}
selectSearch();



function Search(title,search_name){
	console.log(title);
	console.log(search_name);
	

	$.ajax({
        url: severUrl+'index.php?m=Web&a=product_search&search_type='+title+'&search_name='+search_name,
        type: 'GET',
        dataType: 'JSONP'
    })
    .done(function(data) {
    	console.log(data);
      our_productsFun(data.products)
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
}












// best_sellres
	function best_sellresFun(data){
	var SweiperBomCn = $('#SweiperBomCn');
		var html = '';
		for (var i = 0; i < data.length; i++) {
			html += '<div class="swiper-slide"><a href="article.html?article_id='+data[i].article_id+'"> <div class="imgC"><img src="'+data[i].image_url+'"></div><p>'+data[i].title+'</p></a></div>';
		}
		SweiperBomCn.html(html);

		SweiperBom();
		function SweiperBom(){

		var SELLERS_Swiper = new Swiper ('#SELLERS_Swiper', {
            freeMode:true,
            slidesPerView : 'auto'
        }) 

		}
	}
// best_sellres end


})